﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;


namespace ArtLand.OA.Project.ADM
{
    public partial class Adm_InrecdChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               // string deleteScript = GetDeleteScript();
               // JObject defaultObj = new JObject();
               // defaultObj.Add("MATNUB", "");
               // defaultObj.Add("MatterModel", "");
               // defaultObj.Add("MatterSpec", "");
               // defaultObj.Add("MatterColor", "");
               // defaultObj.Add("Quantity", 0);
               // defaultObj.Add("MatterPrice", 0);
               //// defaultObj.Add("Delete", String.Format("<a href=\"javascript:;\" onclick=\"{0}\"><img src=\"{1}\"/></a>", deleteScript, IconHelper.GetResolvedIconUrl(Icon.Delete)));

              //  btnNew.OnClientClick = Grid1.GetAddNewRecordReference(defaultObj, false);

                
                txt_MATNUB.OnClientTriggerClick = Window1.GetSaveStateReference(txt_MATNUB.ClientID)
                      + Window1.GetShowReference("/ADM/Adm_BASICDView.aspx?t=1");
                DataBaseHelper.BindDrp(Drp_InrecdType, EnumData.DataBaseEunm.InrecdType);
                FormInfo.ShowTriggerWin(Window1, txt_MatterAdder, txt_MatterAdderID, "/PublicModel/SelectUsers.aspx?single=1", "选择领料人");
                LoadData();
            }
        }
        // 删除选中行的脚本
        private string GetDeleteScript()
        {
            return Confirm.GetShowReference("删除选中行？", String.Empty, MessageBoxIcon.Question, Grid1.GetDeleteSelectedReference(), String.Empty);
        }
        protected void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            if (!string.IsNullOrEmpty(Request["matterid"]))
            {
                Adm_Matter matter=TbsDbSession.Default.From<Adm_Matter>().Where(Adm_Matter._.ID==int.Parse(Request["matterid"])).ToFirst();

                List<V_MatterBase> amatter = TbsDbSession.Default.From<V_MatterBase>().Where(V_MatterBase._.MatterID == int.Parse(Request["matterid"])).ToList();

                Grid1.DataSource = amatter;
                Grid1.DataBind();
                txt_MatterAdder.Text = matter.MatterAdder;
                txt_MatterAdderID.Text = matter.MatterAddid.Value.ToString();
                txt_NeedTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                txt_REMARK.Text = matter.Remark;
                // txt_MATNUB.Text = amatter.MATNUB;
                //txt_MATNUBID.Text = amatter.MATNUBID.ToString();
                if (matter.MatterType == 1)
                {
                    Drp_InrecdType.SelectedIndex = 1;
                }
                else

                    Drp_InrecdType.SelectedIndex =0;
                Drp_InrecdType.Enabled = false;
            }


            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Adm_INRECD inr = TbsDbSession.Default.From<Adm_INRECD>().Where(Adm_INRECD._.ID == int.Parse(Request["id"])).ToFirst();
                if (inr != null)
                {
                    List<V_InrecdBase> amatter = TbsDbSession.Default.From<V_InrecdBase>().Where(V_InrecdBase._.InrecdID== int.Parse(Request["id"])).ToList();

                    Grid1.DataSource = amatter;
                    Grid1.DataBind();
                    txt_MatterAdder.Text = inr.AddName;
                    txt_MatterAdderID.Text = inr.AddUid.Value.ToString();
                    txt_NeedTime.Text = inr.AddTime.Value.ToString("yyyy-MM-dd");
                    txt_REMARK.Text = inr.REMARK;
                    // txt_MATNUB.Text = amatter.MATNUB;
                    //txt_MATNUBID.Text = amatter.MATNUBID.ToString();
                    Drp_InrecdType.SelectedValue = inr.InOutType;
                    tool2.Hidden = true;
                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
        //    List<Dictionary<string, string>> newAddedList = Grid1.GetNewAddedList();
            int returnvalue = 0;
            Adm_INRECD Exp = new Adm_INRECD();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Exp.ID = int.Parse(Request["id"]);
                Exp.Attach();
                Exp.AddName = CookInfo.EAdminTrueName();
                Exp.AddTime = DateTime.Now;
                Exp.AddUid = CookInfo.EAdminid();
                Exp.InOutType = Drp_InrecdType.SelectedValue;
                Exp.REMARK = txt_REMARK.Text;
                returnvalue = TbsDbSession.Default.Update<Adm_INRECD>(Exp);
            }
            else
            {
                Exp.DeAttach();
                Exp.InRecdNO = DFieldSN.SN(EnumData.DataSNEunm.出入库单号);
                Exp.AddName = CookInfo.EAdminTrueName();
                Exp.AddTime = DateTime.Now;
                Exp.AddUid = CookInfo.EAdminid();
                Exp.InOutType = Drp_InrecdType.SelectedValue;
                Exp.REMARK = txt_REMARK.Text;
                returnvalue = TbsDbSession.Default.Insert<Adm_INRECD>(Exp);

                if (!string.IsNullOrEmpty(Request["matterid"]))
                {
                    int matterid = int.Parse(Request["matterid"]);

                    TbsDbSession.Default.Update<Adm_Matter>(Adm_Matter._.Status, "完成", Adm_Matter._.ID == matterid);
                }
                List<Adm_MatterChild> amatter = TbsDbSession.Default.From<Adm_MatterChild>().Where(Adm_MatterChild._.MatterID == int.Parse(Request["matterid"])).ToList();
                foreach (var item in amatter)
                {
                    Adm_BASICD basicd = TbsDbSession.Default.From<Adm_BASICD>().Where(Adm_BASICD._.MaterNo == item.MaterNo).ToFirst();
                    Adm_InrecdChild matterChild = new Adm_InrecdChild();
                    matterChild.DeAttach();
                    matterChild.MaterNo = basicd.MaterNo;
                    matterChild.MatterColor =item.MatterColor;
                    matterChild.MatterModel = item.MatterModel;
                    matterChild.MatterBrand = item.MatterBrand;
                    matterChild.Quantity =item.Quantity;
                    matterChild.MatterPrice = item.MatterPrice.Value;
                    matterChild.InrecdUnit = basicd.MaterUnit;
                    matterChild.InrecdTitle = basicd.DESTXT;
                    matterChild.InrecdID = returnvalue;
                    TbsDbSession.Default.Insert<Adm_InrecdChild>(matterChild);
                    int num = 0;
                    if (Drp_InrecdType.SelectedValue == "出库")
                        num = basicd.INVQTY.Value - item.Quantity.Value;
                    else
                        num = basicd.INVQTY.Value + item.Quantity.Value;


                    TbsDbSession.Default.Update<Adm_BASICD>(Adm_BASICD._.INVQTY, num, Adm_BASICD._.MaterNo == basicd.MaterNo);
                }

                //foreach (var item in newAddedList)
                //{
                //    List<string> ls = StringHelper.GetSubStringList(item["MATNUB"], '-');
                //    Adm_BASICD basicd = TbsDbSession.Default.From<Adm_BASICD>().Where(Adm_BASICD._.MATNUB == ls[0].Trim()).ToFirst();
                //    Adm_InrecdChild matterChild = new Adm_InrecdChild();
                //    matterChild.DeAttach();
                //    matterChild.MATNUB = basicd.MATNUB;
                //    matterChild.MatterColor = item["MatterColor"];
                //    matterChild.MatterModel = item["MatterModel"];
                //    matterChild.MatterSpec = item["MatterSpec"];
                //    matterChild.Quantity = int.Parse(item["Quantity"]);
                //    matterChild.InrecdPrice = decimal.Parse(item["MatterPrice"]);
                //    matterChild.InrecdUnit = basicd.BAUNIT;
                //    matterChild.InrecdTitle = basicd.DESTXT;
                //    matterChild.InrecdID = returnvalue;
                //    TbsDbSession.Default.Insert<Adm_InrecdChild>(matterChild);


                //    //int num = 0;
                //    //if (Drp_InrecdType.SelectedValue == "出库")
                //    //    num = basicd.INVQTY.Value - int.Parse(item["Quantity"]);
                //    //else
                //    //    num = basicd.INVQTY.Value + int.Parse(item["Quantity"]);


                //    //TbsDbSession.Default.Update<Adm_BASICD>(Adm_BASICD._.INVQTY, num, Adm_BASICD._.MATNUB == basicd.MATNUB);
                //}
                //Adm_BASICD basefirst = TbsDbSession.Default.From<Adm_BASICD>().Where(Adm_BASICD._.MATNUB == txt_MATNUBID.Text).ToFirst();
                

            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
            }
        }
    }
}